package com.atguigu.dga230301.meta.service.impl;

import com.atguigu.dga230301.meta.bean.TableMetaInfo;
import com.atguigu.dga230301.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga230301.meta.constant.MetaConst;
import com.atguigu.dga230301.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga230301.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author zhangjunyi
 * @since 2023-07-01
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    @Override
    public void genExtraListByMetaList(List<TableMetaInfo> tableMetaInfoList) {
        //创建集合用来存放辅助信息对象
        ArrayList<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>(tableMetaInfoList.size());

        //1.如果该表是首次提取，可以初始化比如如一条大部分为空值的辅助信息
        //思考 如何知道这个表是否是首次提取？
        //通过表名和库名查看是否已经有辅助信息了，如果有的话证明这个表的元数据不是第一次提取，那么就不需要再初始化辅助信息了
        //如果没有辅助信息 证明这个表的元数据是第一次提取，那么还需要初始化辅助信息
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            //2.根据表名和库名查询辅助信息表中是否有对应的辅助信息
            TableMetaInfoExtra tableMetaInfoExtra = getOne(new QueryWrapper<TableMetaInfoExtra>().eq("schema_name", tableMetaInfo.getSchemaName()).eq("table_name", tableMetaInfo.getTableName()));
            //3.判断根据库名和表名获取到的辅助信息是否为空
            if (tableMetaInfoExtra==null){
                //4.初始化辅助信息
                tableMetaInfoExtra = new TableMetaInfoExtra();

                //设置表名 库名
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());

                //设置存储周期类型
                tableMetaInfoExtra.setLifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET);

                //设置安全级别
                tableMetaInfoExtra.setSecurityLevel(MetaConst.SECURITY_LEVEL_UNSET);

                //设置层级
                String tableName = tableMetaInfo.getTableName();
                tableMetaInfoExtra.setDwLevel(getInitDwLevelByTableName(tableName));

                tableMetaInfoExtra.setCreateTime(new Date());

                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }

        //将辅助信息保存到Mysql
        saveBatch(tableMetaInfoExtras);
    }

    private String getInitDwLevelByTableName(String tableName){
        if(tableName.startsWith("ods")){
            return MetaConst.DW_LEVEL_ODS;
        } else if (tableName.startsWith("dwd")) {
            return MetaConst.DW_LEVEL_DWD;
        }else if (tableName.startsWith("dim")) {
            return MetaConst.DW_LEVEL_DIM;
        }else if (tableName.startsWith("dws")) {
            return MetaConst.DW_LEVEL_DWS;
        }else if (tableName.startsWith("ads")) {
            return MetaConst.DW_LEVEL_ADS;
        }else if (tableName.startsWith("dm")) {
            return MetaConst.DW_LEVEL_DM;
        }else  {
            return MetaConst.DW_LEVEL_OTHER;
        }
    }

}
